var moduleCode = getQueryString("moduleCode")
$(function () {
    $("#jqGrid").Grid({
        url: '../modulpositioncorrelation/list?moduleCode=' + moduleCode,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '备件名称', name: 'positionName', index: 'POSITION_NAME', width: 120},
            {label: '位置号', name: 'positionNo', index: 'POSITION_NO', width: 70},
            {label: '规格', name: 'spec', index: 'SPEC', width: 60},
            {label: '类别', name: 'modType', index: 'MOD_TYPE', width: 60},
            {label: '特殊处理', name: 'positionRemark', index: 'POSITION_REMARK'},
            {label: '类型', name: 'modType', index: 'MOD_TYPE', width: 60 },
            {label: '寿命标准', name: 'lifeStandard', index: 'LIFE_STANDARD', width: 60},
            {label: '更换日期', name: 'changeTime', index: 'CHANGE_TIME', width: 100},
            {label: '剩余寿命', name: 'residualLife', index: 'RESIDUAL_LIFE', width: 60},
            {label: '套数(自动累加)', name: 'setNum', index: 'SET_NUM', width: 80},
            {label: '', name: 'currentTime', index: 'CURRENT_TIME', width: 80,hidden: true},
            {label: '当前次数', index: 'CURRENT_TIME', width: 60,
                formatter: function (value, col, row) {
                    var stan = row.lifeStandard;
                    var currentTime = row.currentTime;
                    if(currentTime==null||currentTime==""||stan==null||stan==""){
                        return 0;
                    }
                    var ys = stan*0.8;
                    if(currentTime<ys){
                        return currentTime;
                    }else if(currentTime>=ys &&currentTime<=stan){
                        return '<span style="color: orange">'+ currentTime +'</span>';
                    }else {
                        return '<span style="color: red">'+ currentTime +'</span>';
                    }
                }
            },
            {name: 'currentTime', hidden: true}
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        modulPositionCorrelation: {
            updateUser: '',
            updateTime: '',
            delFlag: '',
            creater: '',
            createTime: '',
            modType: '',
            estimateNum: '',
            lifeStandard: '',
            currentTime: '',
            replaceTimes: '',
            setNum: '',
            residualLife: '',
            changeTime: '',
            positionRemark: '',
            positionName: '',
            modulCode: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            modType: '',
        },
        moduleTypeList: enumsToList('MODULE_SPAREPART'), //模具 备件更换 镶块、型芯、镶件
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.modulPositionCorrelation = {
                updateUser: '',
                updateTime: '',
                delFlag: '',
                creater: '',
                createTime: '',
                modType: '',
                estimateNum: '',
                lifeStandard: '',
                currentTime: '',
                replaceTimes: '',
                setNum: '',
                residualLife: '',
                changeTime: new Date(),
                positionRemark: '',
                positionName: '',
                modulCode: moduleCode,
                id: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.modulPositionCorrelation.id == (null || '') ? "../modulpositioncorrelation/save" : "../modulpositioncorrelation/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.modulPositionCorrelation),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulpositioncorrelation/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulpositioncorrelation/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.modulPositionCorrelation = r.modulPositionCorrelation;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'modType':vm.q.modType},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        goModuleReplacePart: function (modulCode) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-pencil"></i> 更换记录',
                content: '../module/modulereplacepart.html?id='+'&modulCode=' + modulCode
            })
        }
    }
});
